package service

import (
	"Short_chain_cats/shortlink/model"
	"context"
)

type LinksService struct{}

func NewLinksService() *LinksService {
	return &LinksService{}
}

// Check 查看某一个长链
func (r LinksService) Check(ctx context.Context, longUrl string) (*model.Links, error) {
	links, err := model.LinksModel.CheckLinks(ctx, longUrl)
	if err != nil {
		return nil, err
	}
	return links, nil
}

// Create 生成新的短链
func (r LinksService) Create(ctx context.Context, linksStore *model.GenerateLinksResp) (*model.Links, error) {
	links, err := model.LinksModel.Create(ctx, linksStore)
	if err != nil {
		return nil, err
	}
	return links, nil
}

// View 查看我的所有短链
func (r LinksService) View(ctx context.Context, req model.ViewLinksReq) (*model.ViewLinksResp, error) {
	res, err := model.LinksModel.ViewLinks(ctx, req)
	if err != nil {
		return nil, err
	}

	return res, nil

}

// Visit 通过短链查找长链
func (r LinksService) Visit(ctx context.Context, shortUrl string) (*model.ViewLinksMid, error) {
	res, err := model.LinksModel.VisitLinks(ctx, shortUrl)
	if err != nil {
		return nil, err
	}
	return res, nil
}

// Find 查看短链状态，未删除改为删除
func (r LinksService) Find(ctx context.Context, id int64) error {
	/*	res, err := model.LinksModel.FindLinks(ctx, id)
		if err != nil {
			return err
		}
		if res.IsDelete == 0 {}*/
	err := model.LinksModel.UpdateLinks(ctx, id)
	if err != nil {
		return err
	}
	return nil
}

// Publish 发布短链
func (r LinksService) Publish(ctx context.Context, links *model.Links) (*model.ViewLinksMid, error) {
	res, err := model.LinksModel.PublishLinks(ctx, links)
	if err != nil {
		return nil, err
	}
	return res, nil
}

// Remove 删除短链
func (r LinksService) Remove(ctx context.Context, id int64) error {
	err := model.LinksModel.UpdateLinks(ctx, id)
	if err != nil {
		return err
	}
	return nil
}

// Edit 修改短链
func (r LinksService) Edit(ctx context.Context, links *model.EditLinksModel) error {
	err := model.LinksModel.EditLinks(ctx, links)
	if err != nil {
		return err
	}
	return nil
}
